package com.gitee.mall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * 定单超时 自动释放订单
 * 定单支付失败  可靠消息+死信队列 恢复库存问题
 */
@Configuration
public class MyMQConfig {

    //-------------------订单超时 自动是否订单队列配置-------------------

    /**
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange", true, false);
    }


    /**
     * 死信队列  队列及路由规则
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");        //将死信消息交给那个死信交换机
        arguments.put("x-dead-letter-routing-key", "order.release.order");      //信息消息路由规则
        arguments.put("x-message-ttl", 60000);                                  //消息死信时长

        return new Queue("order.delay.queue", true, false, false, arguments);
    }

    /**
     * 延时后  用户消费的队列
     *
     * @return
     */
    @Bean
    public Queue orderReleaseQueue() {
        return new Queue("order.release.order.queue", true, false, false);
    }

    /**
     * 定单创建延时队列绑定规则
     *
     * @return
     */
    @Bean
    public Binding orderCreateBinding() {
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    /**
     * 定单超时后  用户消费的队列绑定规则
     *
     * @return
     */
    @Bean
    public Binding orderReleaseBinding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order", null);
    }


    //-------------------

    /**
     * 订单释放直接和库存释放进行绑定
     * 对应队列在库存服务创建
     * {@link com.gitee.mall.order.service.impl.OrderServiceImpl#rabbitTemplate}
     * {@link com.gitee.mall.ware.config.MyMQConfig}
     *
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

    //---------秒杀----------

    /**
     * 秒杀订单队列
     *
     * @return
     */
    @Bean
    public Queue orderSeckillOrderQueue() {
        return new Queue("order.seckill.order.queue", true, false, false);
    }

    /**
     * 秒杀订单绑定
     *
     * @return
     */
    @Bean
    public Binding orderSeckillOrderBinding() {
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }

}
